﻿package com.teaphy.coroutinecrawler.channel

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.channels.ticker
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {

    var num = 0

    val tikerChannel = ticker(delayMillis = 1000, initialDelayMillis = 1000)

    tikerChannel.consumeEach {
        if (num > 5) {
            tikerChannel.cancel()
        }

        num++

        println("element: $it")
    }
}

